package luceneInAction.Chap01;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * @desc 《Lucene In Action》第一章，为某个目录下的所有txt文件，建立索引
 * @author hongxingfan
 * @since 2015年1月17日
 */
public class BuildIndex {

	private IndexWriter writer;

	public static final FieldType INDEX_ANALYSED = new FieldType();

	static {
		INDEX_ANALYSED.setIndexed(true);
		INDEX_ANALYSED.setTokenized(true);
		INDEX_ANALYSED.setStored(true);
		INDEX_ANALYSED.freeze();
	}

	public BuildIndex(String fileDir) {
		Directory dir;
		try {
			dir = FSDirectory.open(new File(fileDir));
			IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_3, new StandardAnalyzer());
			writer = new IndexWriter(dir, config);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public int index(String fileName) throws IOException {
		File file = new File(fileName);
		File[] files = file.listFiles();
		for (File f : files) {
			if (!f.isDirectory() && !f.isHidden() && f.getAbsolutePath().indexOf(".txt") > -1) {
				indexFile(f);
			}
		}
		return writer.numDocs();
	}

	public void indexFile(File file) throws IOException {
		System.out.println("Index for:" + file.getAbsolutePath());
		BufferedReader br = new BufferedReader(new FileReader(file));
		String line = null;
		while ((line = br.readLine()) != null) {
			Document doc = getDocument(line);
			writer.addDocument(doc);
		}
		br.close();
	}

	public Document getDocument(String line) {
		// SELECT title,company,place,degree,jobclass FROM jobs LIMIT 100
		String[] cols = line.split("\t");
		Document doc = new Document();
		doc.add(new Field(Constant.TITLE, cols[0], INDEX_ANALYSED));
		doc.add(new Field(Constant.COMPANY, cols[1], INDEX_ANALYSED));
		doc.add(new Field(Constant.PLACE, cols[2], INDEX_ANALYSED));
		doc.add(new Field(Constant.DEGREE, cols[3], INDEX_ANALYSED));
		doc.add(new Field(Constant.JOBCLASS, cols[4], INDEX_ANALYSED));
		return doc;
	}

	public void close() throws IOException {
		writer.close(); // 调用close后索引写入磁盘
	}

	public static void main(String[] args) throws IOException {
		String indexDir = "D:/test/index"; // 索引目录
		BuildIndex index = new BuildIndex(indexDir);
		String dataDir = "D:/test12/"; // 数据目录
		int num = index.index(dataDir);
		index.close();
		System.out.println(num);
	}

}
